Warming up

Another thing you better get used to pretty fast is error messages. Try typing 1/0. Could'nt be clearer. Taking again our universal example in precision 28, type floor(exp(24*pi)) (floor is the mathematicians' integer part, not to be confused with trunc, which is the computer scientists': floor(- 3.4) is equal to -4 while trunc(- 3.4) is equal to -3). You get a more cryptic error message, which you would immediately understand if you had read the additional comments of the preceding section. Since I told you not to read them, the explananation is simply that GP is unable to compute the integer part of exp(24*pi) given only 28 decimals of accuracy, since it has 33 digits.

Some error messages are even much more cryptic than that and are sometimes not so easy to understand (well, it's nothing compared to TEX's error messages! :-).

For instance, try log(x). Not really clear, is it? But no matter, it simply tells you that GP simply does not understand what log(x) is (although it does know the log function).

Now let's try sqrt(-1) to see what error message we get now. Haha! GP even knows about complex numbers, so impossible to trick it that way. Similarly, try typing log(-2), exp(i*pi), iˆi,.... So we have a lot of real and complex analysis at our disposal (note that there is always a specific branch of multivalued complex transcendental functions which is taken, specified in the manual).

Just for fun, let's try 6*zeta(2)/piˆ2. Pretty good? (If you don't know what the Riemann zeta function is don't worry, we won't use it).


Now GP didn't seem to know what log(x) was, although it did know how to compute numerical values of log. This is annoying. Maybe it knows the exponential function? Let's give it a try. Type exp(x). What's this? If you had had any experience with other systems, the answer should have simply been exp(x) again. But here the answer is the Taylor expansion of the function around x=0, to 16 terms (16 is the default serieslength, which can be changed by typing serieslength=n, where n is the number of terms that you want in your power series (note the O(xˆ16) which ends the series, and which is trademark of this type of object in GP. It is the familiar ``bigoh'' notation of analysis).

You will thus automatically get the Taylor expansion of any function that can be expanded around x=0, and incidentally this explains why we weren't able to do anything with log(x) which is not defined at 0. By if we try log(1+x), then it works. But what if we wanted the expansion around a point different from 0? Well, you're able to change x into x-a, aren't you? So for instance you can type log(x+2) to have the expansion of log around x=2.

As exercises, try cos(x), cos(x)ˆ2+sin(x)ˆ2, exp(cos(x)), exp(exp(x)-1), gamma(1+x), with different values of serieslength if you like.

Let's try something else: type (1+x)ˆ3. No bigoh here, since the result is a polynomial. Haha, but I have learnt that if you do not take exponents which are integers greater or equal to 0, you obtain a power series with an infinite number of nonzero terms. Let's try. Type (1+x)ˆ(-3) (the parentheses around -3 are not necessary but make things easier to read). Surprise! Contrary to what we expected, we don't get a power series but a rational function. Again this is for the same reason that 1/7 just gave you 1/7: the result being exact, PARI doesn't see any reason to make it nonexact.

But I still want that power series. To obtain it, just do as in the 1/7 example: type (1+x)ˆ(-3)+O(xˆ16), (1+O(xˆ16))*(1+x)ˆ(-3), (1+x+O(xˆ16))ˆ(-3),etc...

Now try (1+x)ˆ(1/2). Now we obtain a power series, since the result is an object which PARI does not know how to represent exactly (we could teach PARI about algebraic functions, but then take (1+x)ˆpi as another example). This gives us still another solution to our preceding exercise: we can type (1+x)ˆ(-3.). Since -3. is not an exact quantity, PARI has no means to know that we are dealing with a rational function, and will instead give you the power series.

Finally, if you want to be really fancy, you can type taylor((1+x)ˆ(-3),x) (look at the entry for taylor for the description of the syntax), but this is in fact almost never used.

To summarize, in this section we have seen that in addition to integers, real numbers and rational numbers, PARI can handle complex numbers, polynomials, power series, rational functions. A large number of functions exist which handle these types, but in this tutorial we will only look at a few.


Additional comments

As before, you are supposed to skip this at first, and come back later.

1) A complex number has a real part and an imaginary part (who would have guessed?). However, beware that when the imaginary part is the exact integer zero, it is not printed, but the complex number is not converted to a real number. Hence it may look like a real number without being one, and this may cause some confusion in programs which expect real numbers. For example, type 3+i-i. The answer is, as expected 3. But it is still a complex number. Hence if you then type (1+x)ˆ%, instead of getting the polynomial 1+3*x+3*xˆ2+xˆ3 as expected, you obtain a power series. Worse, when you try to apply an arithmetic function, say the Euler phi function, you get the error message which says that ``arithmetic functions want integer arguments'', which you would have guessed yourself, but the message is difficult to understand since 3 looks like a genuine integer!!! (Please read again if this is not clear. It is a common source of incomprehension).

Similarly, 3+x-x is not the integer 3 but the constant polynomial equal to 3.

If you want the final expression to be in the simplest form possible (for example before applying an arithmetic function, or simply because things will go faster afterwards), apply the function simplify to the result.

2) As already stated, power series expansions are always implicitly around x=0. If you want them around x=a, replace x by z+a in the function that you want to expand. For example, to have the expansion of exp(x) around x=1, we simply type exp(z+1), where z stands for x-a. For complicated functions, it may be simpler to use the substitution function subst. For example, if p=1/(xˆ4+3*xˆ3+5*xˆ2-6*x+7), you may not want to retype this, replacing x by z+a, so you can write subst(p,x,z+a) (look up the exact description of the subst function).

Now try typing p=1+x+xˆ2+O(xˆ10), then subst(p,x,z+1). Do you understand why you get an error message?